padVector <- function(vector,desiredLength){
  if(length(vector) < desiredLength){
    paddingLength = desiredLength - length(vector)
    return(append(vector,rep(vector[length(vector)],paddingLength)))
  }
  return(vector)
}

extractData <- function(inputFile){
  #Read data and compute errors for each cluster
  inputData <- read.table(inputFile,header=TRUE)
  return(inputData)
}

plotMaxError <- function(integratedErrorsPlot,experimentDuration){
  #Plot max error 
  pdf("MaxError.pdf")
  xRange <- range(0,dim(integratedErrorsPlot)[1])
  yRange <- range(0,8)
  nPlots <- dim(integratedErrorsPlot)[2]
  plotColors <- rainbow(nPlots) 
  lineType <- c(1,2,4,2,1,5) 
  plotChar <- seq(18,18+nPlots,1)
  
  plot(xRange,yRange, type="n", xlab="Time (s)", ylab=expression(max(e[i](t),i)),main=expression(max(e[i](t),i) ~ "across" ~ length(resultsFiles) ~"trials"))   
  
  #Add lines
  matlines(integratedErrorsPlot, type="l" , lwd=1.5, lty=lineType, col=plotColors, pch=plotChar)
  
  #Add a title
  #title(graphTitle)
  
  #Add a legend 
  legend(x="bottomleft", colnames(integratedErrorsPlot), cex=0.8, col=plotColors, lty=lineType, title="Percentiles")
  dev.off()
  
  write.table(integratedErrorsPlot,"MaxErrorPlots.txt",sep="\t",row.names=FALSE,col.names=TRUE)
  #plot.ts(meanIntegratedMaxError,ylab=expression(max(e[i](t),i)))
  #title(str(graphTitle))
  #dev.off()
}

plotRobotStates <- function(robotStates){
  #Plot max error 
  pdf("RobotStates.pdf")
  graphTitle <- paste("Number of allocated robots across",length(resultsFiles),"trials")
  xRange <- range(0,dim(robotStates)[1])
  yRange <- range(0,20)
  nPlots <- 3
  plotColors <- rainbow(nPlots) 
  lineType <- c(1,2,4,2,1) 
  plotChar <- seq(18,18+nPlots,1)
  
  plot(xRange,yRange, type="n", xlab="Time steps", ylab="Robots")   
   
  #Add lines
  matlines(workingPlot, type="l" , lwd=1.5, lty=lineType, col=plotColors, pch=plotChar)
  
  #Add a title
  title(graphTitle)
  
  #Add a legend 
  legend(x="bottomright", colnames(workingPlot), cex=0.8, col=plotColors, lty=lineType, title="Percentiles")
  dev.off()
  
}

#############################################################################

# $1 - Directory where the .occ files corresponding to the different experiments are stored
# $2 - Experiment duration (seconds)
args <- commandArgs(trailingOnly = TRUE)
workingDirectory <- args[1]
rm(args)

#Set the directory containing the experiment results as current working directory
setwd(paste("/home/deste/argos2/user/jdestefani/results/Boxplots/",workingDirectory,"/",sep=""))
#List all the files containing information about occupation
resultsFiles <- list.files(pattern = "\\.bp$")
print(resultsFiles)

print("[STATUS] - Processing results file")
#Process every single file separately
boxplotData <- sapply(resultsFiles,extractData)
#boxplotData <- data.frame(matrix(unlist(boxplotData), ncol=length(boxplotData)))

print("[STATUS] - Producing plots")
#plotMaxError(integratedErrors)
pdf(paste(workingDirectory,"pdf",sep="."))
#Scenario A/B
boxplot(as.data.frame(boxplotData),names=c("Improved","Informed","Naive"),horizontal=TRUE,main=paste("Completion time -",workingDirectory),col=terrain.colors(3))
#Scenario A & B
#boxplot(as.data.frame(boxplotData),names=c("A -Im","B - Im","A -In","B - In", "A - N","B - N"),horizontal=TRUE,main=paste("Completion time -",workingDirectory),col=terrain.colors(6),las=1)

dev.off()
rm(resultsFiles)

#createTexGraph("t","$\\max_i(e_i(t))$",5,5,inputData[,1],maxErrorIntegrated[,1])
